<?php
namespace app\dsadmin\model;
use think\Model;
use think\Db;
use think\Cache;
class MemberModel extends Model
{
    protected $name = 'member';
    protected $autoWriteTimestamp = true; // 开启自动写入时间戳
    
    /**
     * 根据搜索条件获取用户列表信息
     */
    public function getMemberByWhere($map, $Nowpage, $limits)
    {
        return $this->where($map)->page($Nowpage, $limits)->order('id desc')->select();
    }
     public function shengorderwhere($map, $Nowpage, $limits)
    {
        return Db::name('order_bi')->where('zfcha > 0')->page($Nowpage, $limits)->order('zfcha desc')->select();
        // return $this->where($map)->page($Nowpage, $limits)->order('id desc')->select();
    }
    public function jiangorderwhere($map, $Nowpage, $limits)
    {
        return Db::name('order_bi')->where('zfcha < 0')->page($Nowpage, $limits)->order('zfcha asc')->select();
        // return $this->where($map)->page($Nowpage, $limits)->order('id desc')->select();
    }

    /**
     * 根据搜索条件获取所有的用户数量
     * 
     * @param  $where 
     */
    public function getAllCount($map)
    {
        return $this->where($map)->count();
    }

    /**
     * 插入信息
     */
    public function insertMember($param)
    {
        try
        {
            $param['password'] = md5($param['password']);
            $param['txpassword'] = md5($param['txpassword']);
            $result = $this->validate('MemberValidate')->allowField(true)->save($param);
            $userid = Db::name('member')->getLastInsID();
            $member = Db::name("member")->where(['id' => $userid])->find();
            Cache::store('redis')->set("ylmoban".$userid,0);
            Cache::store('redis')->set("ylmember".$userid,json_encode($member),77879680);
            $money = 5.00;
            Db::name('kou')->insert(['userid' => $userid, 'cs' => 30, 'ns' => 30]);
            Db::name('pian')->insert(['userid' => $userid, 'money' => $money,'time' => time()]);
             Cache::store('redis')->set("ylmoney".$userid,$money,77879680);
            if (false === $result)
            {
                return ['code' => -1, 'data' => '', 'msg' => $this->getError()];
            }
            else
            {
                return ['code' => 1, 'data' => '', 'msg' => '添加成功'];
            }
        }
        catch(PDOException $e)
        {
            return ['code' => -2, 'data' => '', 'msg' => $e->getMessage()];
        }
    }

    /**
     * 编辑信息
     * 
     * @param  $param 
     */
    public function editMember($param)
    {
        
        
        if(isset($param['money'])){
            
            echo   1111;
            die;
            
        }
        try
        {
            $result = $this->validate('MemberValidate')->allowField(true)->save($param, ['id' => $param['id']]);
            if (false === $result)
            {
                return ['code' => 0, 'data' => '', 'msg' => $this->getError()];
            }
            else
            {
                return ['code' => 1, 'data' => '', 'msg' => '编辑成功'];
            }
        }
        catch(PDOException $e)
        {
            return ['code' => 0, 'data' => '', 'msg' => $e->getMessage()];
        }
    }

    /**
     * 根据管理员id获取角色信息
     * 
     * @param  $id 
     */
    public function getOneMember($id)
    {
        return $this->where('id', $id)->find();
    }

    /**
     * 删除管理员
     * 
     * @param  $id 
     */
    public function delUser($id)
    {
        try
        {
            $this->where('id', $id)->delete();
            Db::name('auth_group_access')->where('uid', $id)->delete();
            return ['code' => 1, 'data' => '', 'msg' => '删除成功'];
        }
        catch(PDOException $e)
        {
            return ['code' => 0, 'data' => '', 'msg' => $e->getMessage()];
        }
    }

    public function delMember($id)
    {
        try
        {
            $this->where(['id' => $id])->delete();
            return ['code' => 1, 'data' => '', 'msg' => '删除成功'];
        }
        catch(PDOException $e)
        {
            return ['code' => 0, 'data' => '', 'msg' => $e->getMessage()];
        }
    }
    
    
    public function edittxpassword($id,$txpassword)
    {
        try
        {
            $this->where(['id' => $id])->update(['txpassword'=>$txpassword]);
            return ['code' => 1, 'data' => '', 'msg' => '修改成功'];
        }
        catch(PDOException $e)
        {
            return ['code' => 0, 'data' => '', 'msg' => $e->getMessage()];
        }
    }
    
    public function editpassword($id,$password)
    {
        try
        {
            $this->where(['id' => $id])->update(['password'=>$password]);
            return ['code' => 1, 'data' => '', 'msg' => '修改成功'];
        }
        catch(PDOException $e)
        {
            return ['code' => 0, 'data' => '', 'msg' => $e->getMessage()];
        }
    }
    
    
    public function delMembertxmoney()
    {
        try
        {
            $this->where(['txmoney' => ["<>", 0]])->update(['txmoney' => '0']);
            return ['code' => 1, 'data' => '', 'msg' => '清除成功'];
        }
        catch(PDOException $e)
        {
            return ['code' => 0, 'data' => '', 'msg' => $e->getMessage()];
        }
    }
}
